package com.scut.edu.wanyou;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.scut.edu.wanyou.conf.Configuration;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
public class BaseApp extends Application {
    public static Context context;
    private static Handler handler;
    public static Configuration conf ;
    private List<Activity> activityList = new LinkedList<Activity>();
    private static BaseApp instance = null;
    /**
     * Description:
     * <p>加载配置文件，初始化配置</p>
     * Date:2012-6-10
     * @author chenxc
     * @return void
     */
    public void init(){
        BaseApp.conf = new Configuration(this);
    }

    /**
     * Description:
     * <p>为程序的第一次运行做配置准备<p>
     * Date:2012-6-10
     * @author chenxc
     * @return void
     */
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        context = getBaseContext();
        handler = new Handler();
        init();
        TempValue.VALUES = new HashMap<String, Object>();

    }

    public static void showToast(final int resId) {
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static void showToast(final String text) {
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static BaseApp getInstance()
    {
        if(null == instance)
        {
            instance = new BaseApp();

        }
        return instance;
    }

    public void addActivity(Activity activity)
    {
        activityList.add(activity);
    }

    public void exit()
    {
        for(Activity activity:activityList)
        {
            activity.finish();
        }
        System.exit(0);
    }
}